<?php
/**
 * Created by PhpStorm.
 * User:lyq
 * Date: 2018/8/22
 * Time: 13:49
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
use app\common\model\Message;

class Notice extends Common
{
    public function index()
    {
        //标题
        $this->templateTitle("公告列表");

        $where = [];

        $id = request()->param("id");
        $this->assign("id", $id);
        if ($id) {
            $where["id"] = $id;
        }

        $condition = request()->param("condition");
        $this->assign("condition", $condition);
        if ($condition) {
            $where["title"] = ["LIKE", "%$condition%"];
        }
        $is_visible = request()->param('is_visible');
        if ($is_visible != '') {
            $where['is_visible'] = $is_visible;
        }
        $this->assign('is_visible', $is_visible);

        //获取所有用户
        $list = Db::name("notice")
            ->where($where)
            ->order("display_order DESC,id DESC")
            ->paginate(15);

        $this->assign("list", $list);
        return $this->fetch();
    }

    //添加
    public function add()
    {
        if (request()->isGet()) {
            $this->layoutModal();
            $this->templateTitle('添加公告');
            return $this->fetch();
        }

        if (request()->isAjax()) {
            //创建数据
            $data = array(
                "create_time" => now_datetime(),
                "update_time" => now_datetime()
            );
            $data["title"] = trim(request()->param("title"));
            if (empty($data['title'])) {
                return $this->ajaxError('请输入标题');
            }
            //排序
            $data["display_order"] = request()->param("ord");
            //是否显示
            $data["is_visible"] = request()->param("visible");

            //详情&简介
            $detail = request()->param("detail");
            if (empty($detail)) {
                return $this->ajaxError('请输入公告详情');
            }

            $description = trim(request()->param('description'));
            if (empty($description)) {
                $description = remove_all_tags($detail);
            }
            $data['description'] = $description;


            try {
                $detail_id = Db::name('notice_detail')->insertGetId(['detail' => $detail]);
                $data['detail_id'] = $detail_id;
                Db::name('notice')->insert($data);
                /*操作记录*/
                Message::admin_log(0, "后台添加公告！");
                return $this->ajaxSuccess("添加成功！");
            } catch (Exception $exception) {
                global $admin_has_exception;
                $admin_has_exception = true;
                return $this->ajaxError($exception->getMessage());
            }

        }
    }

    //编辑
    public function edit()
    {
        //ID
        $id = request()->param("id");
        //获取信息
        $info = Db::name("notice")
            ->alias('no')
            ->join('__NOTICE_DETAIL__ nd', 'nd.id=no.detail_id', 'LEFT')
            ->field('no.*,nd.detail')
            ->where("no.id", $id)
            ->find();

        if (request()->isGet()) {
            $this->templateTitle('编辑公告');
            $this->layoutModal();
            $this->assign("ueditor_content", array("detail" => $info['detail']));
            $this->assign("info", $info);
            return $this->fetch();
        }

        if (request()->isAjax()) {
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );
            $data["title"] = trim(request()->param("title"));
            if (empty($data['title'])) {
                return $this->ajaxError('请输入标题');
            }

            //排序
            $data["display_order"] = request()->param("ord");
            //是否显示
            $data["is_visible"] = request()->param("visible");

            //详情&简介
            $detail = request()->param("detail");
            if (empty($detail)) {
                return $this->ajaxError('请输入公告详情');
            }
            $description = trim(request()->param('description'));
            if (empty($description)) {
                $description = remove_all_tags($detail);
            }
            $data['description'] = $description;

            try {
                Db::name('notice_detail')->where('id', $info['detail_id'])->update(['detail' => $detail]);
                Db::name('notice')->where('id', $id)->update($data);
                /*操作记录*/
                Message::admin_log(0, "后台公告编辑通知！id:" . $id);
                return $this->ajaxSuccess("编辑成功！");
            } catch (Exception $exception) {
                global $admin_has_exception;
                $admin_has_exception = true;
                return $this->ajaxError($exception->getMessage());
            }

        }
    }

    //删除
    public function remove()
    {
        if (request()->isAjax()) {
            //获取ID
            $id = request()->param("id");

            $info = Db::name('notice')->where('id', $id)->find();

            try {
                Db::name('notice')
                    ->where('id', $id)
                    ->delete();
                Db::name('notice_detail')
                    ->where('id', $info['detail_id'])
                    ->delete();
                Db::name('notice_reader')
                    ->where('nid', $id)
                    ->delete();
                /*操作记录*/
                Message::admin_log(0, "后台删除通知！id:" . $id);
                return $this->ajaxSuccess("删除成功！");
            } catch (Exception $exception) {
                global $admin_has_exception;
                $admin_has_exception = true;
                return $this->ajaxError($exception->getMessage());
            }

        }
    }

    //更改通知显示隐藏
    public function visible()
    {
        if (request()->isAjax()) {
            //通知ID
            $id = request()->param("id");
            //获取可见性状态
            $status = request()->param("status");

            try {
                //修改通知显示隐藏
                $save = Db::name("notice")
                    ->where("id", $id)
                    ->setField("is_visible", $status);

                if ($save) {
                    /*操作记录*/
                    Message::admin_log(0, "后台修改通知的状态！id:" . $id);
                    return $this->ajaxSuccess("设置通知显示或隐藏成功！");
                } else {
                    return $this->ajaxError("设置通知显示或隐藏失败！");
                }
            } catch (Exception $exception) {
                return $this->ajaxError($exception->getMessage());
            }
        }
    }
}